package com.example.demo.controller;

import com.example.demo.entity.Account;
import com.example.demo.entity.Temp;
import com.example.demo.repository.AccountRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class indexController {
    @Autowired
    private AccountRepository accountRepository;
    @RequestMapping("/index")
    public String index(@AuthenticationPrincipal Account account, @ModelAttribute("temp") Temp temp, Model model){
//        System.out.println(account.getUsername());
        model.addAttribute("userName",account.getUsername());
        return "PersonInfo";
    }
    @RequestMapping("/register")
    public String register(@RequestParam("RUserName")String RUserName,@RequestParam("RPassword")String RPassword){
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        String role = "USER";
        Account account = new Account(RUserName,bCryptPasswordEncoder.encode(RPassword),role);
        accountRepository.save(account);
        return "redirect:/";
    }
}
